package com.xiaoshuidi.cloud.module.member.service.securitycheck;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck.vo.SecurityCheckRectificationFileCreateReqVO;
import com.xiaoshuidi.cloud.module.member.convert.securitycheck.SecurityCheckRectificationFileConvert;
import com.xiaoshuidi.cloud.module.member.mapper.securitycheck.SecurityCheckRectificationFileMapper;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheck.SecurityCheckRectificationFileDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.member.enums.ErrorCodeConstants.*;

/**
 * 安全检查整改文件 Service 实现类
 *
 * @author hans.zhang
 */
@Service
@Validated
public class SecurityCheckRectificationFileServiceImpl implements SecurityCheckRectificationFileService {

    @Resource
    private SecurityCheckRectificationFileMapper securityCheckRectificationFileMapper;

    @Override
    public Long createSecurityCheckRectificationFile(SecurityCheckRectificationFileCreateReqVO createReqVO) {
        // 插入
        SecurityCheckRectificationFileDO securityCheckRectificationFile = SecurityCheckRectificationFileConvert.INSTANCE.convert(createReqVO);
        securityCheckRectificationFileMapper.insert(securityCheckRectificationFile);
        // 返回
        return securityCheckRectificationFile.getId();
    }


    @Override
    public Boolean deleteSecurityCheckRectificationFile(Long id) {
        // 校验存在
        validateSecurityCheckRectificationFileExists(id);
        // 删除
        securityCheckRectificationFileMapper.deleteById(id);
        return Boolean.TRUE;
    }

    private void validateSecurityCheckRectificationFileExists(Long id) {
        if (securityCheckRectificationFileMapper.selectById(id) == null) {
            throw exception(SECURITY_CHECK_RECTIFICATION_FILE_NOT_EXISTS);
        }
    }


    @Override
    public List<SecurityCheckRectificationFileDO> getSecurityCheckRectificationFileList(Collection<Long> ids) {
        return securityCheckRectificationFileMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SecurityCheckRectificationFileDO> getSecurityCheckRectificationFilePage(Long securityCheckId, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<SecurityCheckRectificationFileDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecurityCheckRectificationFileDO::getSecurityCheckId, securityCheckId);
        wrapper.orderByDesc(SecurityCheckRectificationFileDO::getCreateTime);
        Page<SecurityCheckRectificationFileDO> page = securityCheckRectificationFileMapper.selectPage(new Page<>(pageNo, pageSize), wrapper);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }


}
